import sys
import os
import platform

if platform.system() == 'Linux':
    import signal

class Engine:
    def __init__(self):
        pass

    def start(self, configPath, serviceName):
        self.serviceName = serviceName
        param = '--config=' + configPath
        param += ' --name=' + serviceName
        param += ' --daemon'
        if platform.system() == 'Windows':
            os.system('start ..\\..\\engine\\bin\\debug\\engine.exe ' + param)
        else:
            os.system('../../engine/bin/engine ' + param + ' &')
        
    def stop(self):
        file=open(self.serviceName + '.pid', 'r')
        pid=file.read()
        file.close()
        if platform.system() == 'Windows':
            os.system('taskkill.exe /pid:'+pid + ' 2>nul 1>nul')
        else:
            os.system('kill -9 ' + pid)

def checkLog(logName, content, times=0):
    file = open(logName, 'r')
    data = file.read()
    if times == 0:
        return data.find(content) != -1
    else:
        return data.count(content) == times

def writeResult(name, result, error=None):
    file = open(name+'.result', 'w')
    file.write(result)
    if error is not None:
        file.write(',' + error)
    file.close()